<template>
  <div class="form">
    <el-form
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="ruleForm.title" />
      </el-form-item>
      <el-form-item>
        <el-upload
          action="/api/upload"
          list-type="picture-card"
          :on-success="handersuccess"
        >
          <el-icon><Plus /></el-icon>

          <template #file="{ file }">
            <div>
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <el-icon><zoom-in /></el-icon>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleDownload(file)"
                >
                  <el-icon><Download /></el-icon>
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <el-icon><Delete /></el-icon>
                </span>
              </span>
            </div>
          </template>
        </el-upload>

        <el-dialog v-model="dialogVisible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-form-item>

      <el-form-item label="文章描述" prop="title">
        <el-input v-model="ruleForm.title" />
      </el-form-item>
      <el-form-item>
        <Editor :value="emailForm.msg" @updateValue="getMsg" />
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="add">添加文章</el-button>
  </div>
</template>

<script setup lang="ts">
import axios from "axios";
import { ref, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import { useRouter, useRoute } from "vue-router";
import { addList, getinfo } from "../utils/api";
import Editor from "@/components/Editor/index.vue";
import type { UploadFile } from "element-plus";
const router = useRouter();
const emailForm = reactive({
  msg: "",
});
const ruleForm = reactive({
  title: "",
  content: "",
  image: "",
  column: "",
  author: "",
});
// 富文本事件
const getMsg = (val: any) => {
  emailForm.msg = val;
  console.log(val);
  ruleForm.content = val;
};
const dialogImageUrl = ref("");
const dialogVisible = ref(false);
const disabled = ref(false);

const handleRemove = (file: UploadFile) => {
  console.log(file);
};

const handlePictureCardPreview = (file: UploadFile) => {
  dialogImageUrl.value = file.url!;
  dialogVisible.value = true;
};

const handleDownload = (file: UploadFile) => {
  console.log(file);
};

const handersuccess = (res: any) => {
  // console.log(res);
  ruleForm.image = res.data._id;
  console.log(ruleForm.image, "imgId");
};
setTimeout(() => {
  getinfo().then((res) => {
    (ruleForm.author = res.data.data._id),
      (ruleForm.column = res.data.data.column);
    console.log(ruleForm, "ruleForm");
  });
}, 50);
// 添加文章
const add = () => {
  console.log(ruleForm);
  let list = {
    title: ruleForm.title,
    content: ruleForm.content,
    image: ruleForm.image,
    column: ruleForm.column,
    author: ruleForm.author,
  };
  addList(list).then((res) => {
    console.log(res);
    router.push({ path: "/my", query: { id: ruleForm.column } });
  });
};
</script>

<style lang="scss">
.form {
  width: 600px;
  margin: auto;
}
</style>
